package com.servcice.impl;

import com.dao.ClazzDao;
import com.dao.RedisDao;
import com.dao.TeacherDao;
import com.dto.BaseDto;
import com.dto.dataenum.MessageEnum;
import com.entity.Account;
import com.entity.Clazz;
import com.entity.Teacher;
import com.exception.CustomException;
import com.mapper.TeacherMapper;
import com.page.PageInfo;
import com.servcice.TeacherService;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("teacherService")
@EnableTransactionManagement
public class TeacherServiceImpl implements TeacherService {
    @Resource
    TeacherDao teacherDao;
    @Resource
    RedisDao redisDao;

    public BaseDto<Teacher> getAll() throws Exception {

        BaseDto<Teacher> dto = new BaseDto<>(MessageEnum.SELECT_STUDENTALL_SUCCESS);
        dto.settList(teacherDao.getAll());
        return dto;
    }

    public int addTeacher(Teacher teacher) throws Exception {
        return teacherDao.addTeacher(teacher);
    }

    /**
     * 前2页存redis 2页以后的直接查询mysql，dao提供了2个查询方法，一个有缓存注解关联缓存，一个没有缓存注解不关联
     * @param pageInfo
     * @param cacheId
     * @return
     * @throws Exception
     */
    public BaseDto<Teacher> getTeachersByPage(PageInfo pageInfo,String cacheId) throws Exception {
        BaseDto<Teacher> dto = new BaseDto<>(MessageEnum.SELECT_STUDENTBYPAGE_SUCCESS);
        List<Teacher> teachers = pageInfo.getCurrPage()>2?teacherDao.getTeachersByPage(pageInfo):teacherDao.getTeachersByPage(pageInfo,cacheId);//如果大于2 不走缓存
        int itemCount = pageInfo.getCurrPage() > 2?pageInfo.getItemCount():teacherDao.getItemCount("teacherCount");//如果小于2，需要从缓存重新获取当前总记录数
        pageInfo.setItemCount(itemCount);
        dto.settList(teachers);
        dto.setObj(pageInfo);
        return dto;
    }
    public BaseDto<Teacher> searchTeacherByName(String name,PageInfo pageInfo) throws Exception{
        BaseDto<Teacher> dto = new BaseDto<>(MessageEnum.SELECT_STUDENTBYPAGE_SUCCESS);
        List<Teacher> teachers = teacherDao.searchTeacherByName(name,pageInfo);
        //如果学生集合为空 抛出异常
       if(teachers.size() <= 0) {
           throw new CustomException(MessageEnum.SELECT_STUDENTSEARCH_FAIL.getMsg());
       }
        dto.settList(teachers);
        dto.setObj(pageInfo);
        return dto;
    }
    public BaseDto selectTeacherWithScsAndCourse(String sno)throws Exception{
        BaseDto dto = new BaseDto<>(MessageEnum.SELECT_STUDENTBYSNOWITHSCS_SUCCESS);
        dto.setT(teacherDao.selectTeacherWithScsAndCourse(sno));
        return dto;
    }

    @Override
    public Teacher loginTeacherByTname(String tname) throws Exception {
        return teacherDao.loginTeacherByTname(tname);
    }

    public Teacher selectTeacherBySno(String sno){
        return teacherDao.selectTeacherBySno(sno);
    }


    @Override
    public Account getAccountByAccountName(String name) throws Exception {
        return loginTeacherByTname(name);
    }

}
